<?php

declare(strict_types=1);
namespace App\Model\CommodityModel;

use App\Model\BaseModel\BaseCommodityModel;
use Hyperf\DbConnection\Db;
use App\Service\Code\Normal;
use App\Service\OrderService\OrderQueryService\QueryGoodsMicService;
class CommodityArchives extends BaseCommodityModel
{
    public $table = 'commodityarchives_s2b';
    public $goods_types = [
        '0' => '一般贸易',
        '1' => '保税商品',
        '2' => '海外直邮'
    ];
    
    //0-无单位 1-毫升 2-升 3-克 4-千克
    public $weight_unit = [
        '0' => '无单位',
        '1' => '毫升',
        '2' => '升',
        '3' => '克',
        '4' => '千克'
    ];
    
    /**
     * 获取主档案ID
     * @param array $SkuCodes
     */
    public function getSkuIDListBySkuCodes( array $SkuCodes = [] )
    {
        return $this
        ->select([
            Db::raw("ID AS SkuID"),
            Db::raw("SKUCode"),
        ])
        ->whereIn('SKUCode',$SkuCodes)
        ->where('IsUseAble',1)//可以下单
        ->get()
        ->toArray();
    }
    
    /**
     * 根据国际条码查询图片和商品详情
     * @param array $body
     */
    public function queryCommodityImageList( array $body = [] )
    {
        if( empty($body['data']) ) {
            return Normal::returnCode(Normal::FAIL,"data不能为空");
        }
        
        $BarCodes = $body['data'];
        if( !is_array($BarCodes) ) {
            return Normal::returnCode(Normal::FAIL,"data必须为数组");
        }
        
        $BarCodes = array_filter(array_unique($BarCodes));
        if( empty($BarCodes) ) {
            return Normal::returnCode(Normal::FAIL,"data数据异常，不能为空");
        }
        
        $list = $this->from($this->table." as com")
        ->select([
            'com.BarCode',
            'cds.Images',
            'cds.Description',
        ])
        ->join('commoditydescription_s2b as cds',"cds.CommodityInfoID","=","com.ID")
        ->whereIn('com.BarCode',$BarCodes)
        ->where('com.IsIntegrity',1)//商品资料完整
        ->get()
        ->toArray();
        
        //处理图片和详情清单
        $queryGoodsList = [];
        foreach ($list as $item ) 
        {
            $imageList = @json_decode($item['Images'],true);
            $imageList = QueryGoodsMicService::instance()->getResetImagesList($imageList);
            $queryGoodsList[$item['BarCode']] = [
                'barCode' => $item['BarCode'],
                'imageInfoUrls' => $imageList,
                'description'   => $item['Description']
            ];
            unset($item);
        }
        
        rsort($queryGoodsList);
        return Normal::returnCode(Normal::SUC,"获取成功",$queryGoodsList);
    }
}